#include<iostream>
#include<vector>
#include<string>
#include<algorithm>

using namespace std;

struct Ath{
	string name;
	int score;
};

bool cmp(Ath a, Ath b){
	return a.score > b.score;
}

int main(){
	vector<Ath> aths;
	int n;
	cin>>n;
	for(int i=0;i<n;i++){
		Ath t;
		cin>>t.name>>t.score;
		aths.push_back(t);
	}
	
	sort(aths.begin(), aths.end(),cmp);
	
	for(int i=0;i<n;i++){
		cout<<aths[i].name<<endl;
	}
	return 0;
}
